#!/usr/bin/env bash
export HOST1=192.168.1.28
export HOST2=192.168.1.29
count=$(netstat -anpt |grep 6443 |grep LISTEN |wc -l)
if [ $count -gt 0 ];then
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
  kubectl apply -f https://gitee.com/hewei8520/File/raw/master/1.13.4/rbac-kdd.yaml
  kubectl apply -f https://gitee.com/hewei8520/File/raw/master/1.13.4/calico.yaml
  # status
  master_status=$(kubectl get nodes | grep "NotReady" | awk {'print $2'})
  while [[ `kubectl get nodes | grep "NotReady" | awk {'print $2'} |sed -n '1p'` == "NotReady" ]]
  do
    echo "master 节点正在初始化,请稍候"
    sleep 10
  done
else
  echo "master 初始化失败, 请查看日志"
exit
fi
echo "k8s-01 初始化成功"
kubectl get nodes -o wide
# 更新另外2台master,上传证书
kubeadm_join=`kubeadm token create --print-join-command`
for HOST in ${IPS}
do
  ssh ${USER}@${HOST} mkdir -p /etc/kubernetes/
  scp -r /etc/kubernetes/pki ${USER}@${HOST}:/etc/kubernetes/
  ssh ${USER}@${HOST} curl -s https://gitee.com/hewei8520/File/raw/master/1.13.5/base-env-config-point-node.sh |bash
  ssh ${USER}@${HOST} ${kubeadm_join} --experimental-control-plane
done


### 切换kubelet目录
systemctl stop kubelet
systemctl stop docker
sed -i 's/var\/lib/data/g' /lib/systemd/system/kubelet.service.d/10-kubeadm.conf
sed -i  's/Environment="KUBELET_KUBECONFIG_ARGS=/Environment="KUBELET_KUBECONFIG_ARGS=--root-dir=\/data\/kubelet  /g' /lib/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl enable --now docker
systemctl enable --now kubelet



for HOST in ${HOST1} ${HOST2} ${HOST3} ${HOST4} ${HOST5}
do
  ssh ${USER}@${HOST} systemctl stop kubelet
  ssh ${USER}@${HOST} systemctl stop docker
  ssh ${USER}@${HOST} sed -i 's/var\/lib/data/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
  ssh ${USER}@${HOST} sed -i  's/Environment="KUBELET_KUBECONFIG_ARGS=/Environment="KUBELET_KUBECONFIG_ARGS=--root-dir=\/data\/kubelet  /g' /lib/systemd/system/kubelet.service.d/10-kubeadm.conf
  ssh ${USER}@${HOST} mv /var/lib/kubelet /data/
  ssh ${USER}@${HOST} ln -s /data/kubelet /var/lib/kubelet
  ssh ${USER}@${HOST} systemctl start kubelet
  ssh ${USER}@${HOST} systemctl start docker
  ssh ${USER}@${HOST} systemctl enable --now kubelet
done
